﻿// Description: Node 基类

#pragma once

#include "TrafficActorHotArea.hpp"
#include "StartupArguments.hpp"
#include "NodeHotAreaManager.hpp"
#include "cybertron/network/SocketBase.hpp"
#include "cybertron/node/WorkApp.hpp"
#include "cybertron/core/ConfigureClient.hpp"
#include <chrono>

CYBERTRON_BEGIN


class WorkAppWithHotArea : public WorkApp

{
public:
	CybertronCoreAPI WorkAppWithHotArea();
	CybertronCoreAPI virtual ~WorkAppWithHotArea();

	//////////////////////////////////////////////////////
	CybertronCoreAPI virtual int getVerion(int argc, char* argv[]) { return 0; }
	//////////////////////////////////////////////////////

	CybertronCoreAPI virtual bool init(int argc, char* argv[]);

	CybertronCoreAPI virtual bool onLoadConfig();

	CybertronCoreAPI virtual int beforeRun();

	CybertronCoreAPI virtual bool onTaskStartBefore();

	CybertronCoreAPI virtual void clear();

	CybertronCoreAPI virtual void handleTimer();

	CybertronCoreAPI virtual bool getDisconnected() const;

	CybertronCoreAPI bool connectNodeTimer();

	CybertronCoreAPI int getFpsMul10() const;
	// num of threads created for current Node. default to be 1
	CybertronCoreAPI virtual std::uint32_t getNumThreads() const;

	// this function is called in network thread
	CybertronCoreAPI enum EHandleResult
	{
		EHandleResult_Unhandled,
		EHandleResult_Handled
	};

    CybertronCoreAPI bool onTaskRunning() {  return true;  };
	CybertronCoreAPI  bool onTaskStop();

	
protected:
	friend class NodeHotAreaManager;

	// the following methods are called in main thread.
	CybertronCoreAPI virtual bool onConnectNodeTimerAfter();
	CybertronCoreAPI virtual void onTimer(
		int framestamp,
		int ms);
	CybertronCoreAPI virtual void onTimer(
		int framestamp,
		int ms,
		int msModNs);
	CybertronCoreAPI virtual void onPlay();
	CybertronCoreAPI virtual void onPause();
	CybertronCoreAPI virtual void onReset();
	CybertronCoreAPI virtual std::string onControlCommand(
		const std::string controlCommand);
	CybertronCoreAPI virtual void onEnd(); // onEnd will be called when FromAnyRequestTaskEnd received
	CybertronCoreAPI virtual void onEnd(Common::ETaskEndReason reason);  // onEnd will be called when FromAnyRequestTaskEnd received, this CB with reason

	CybertronCoreAPI virtual void onCaseStart();
	CybertronCoreAPI virtual void onCaseStop();

protected:
	// performance warning: this is slow!
	//CybertronCoreAPI void broadcast(const Message& msg); // Broadcast message to all nodes connected to internal server

	//这个回调，来自节点的内部server，处理那些通过直连方式发送过来的消息。
	//这种连接适合发送大数据，不需要时间同步的数据
	CybertronCoreAPI virtual EHandleResult onMessageDirect(
		SocketBasePtr pSender,
		const Message& msg);

	//这个回调，来自节点和时间服务器的连接
	//用于接收从时间服务器发送过来的广播或者sendCommandToAllNodes/sendCommandToNodes消息，适合轻量消息
	CybertronCoreAPI virtual void onMessageCommand(std::uint32_t msgId, const std::string* msgDataBody);

	//从这个回调函数获取订阅数据
	CybertronCoreAPI virtual void onChannelSubData(std::string Channel, std::uint32_t msgId, const std::string* msgDataBody);

	CybertronCoreAPI virtual void onClientConnection(
		SocketBasePtr pClientSocket,
		ESocketStatus status);

private:
	void zeroMembers();
	void clearThis();

private:
	WorkAppWithHotArea(const WorkAppWithHotArea&);
	WorkAppWithHotArea& operator = (const WorkAppWithHotArea&);

protected:
	NodeHotAreaManager mHotAreaManager;

private:
	std::chrono::high_resolution_clock::time_point mPreviousCalculateFpsTime;
	int mNumFramesSincePreviousTime;
	int mFpsMul10;
	std::string mNodeTimerIP;
	int mNodeTimerPort;
	std::shared_ptr<cybertron::ConfigureClient> mpConfigureClient;
};

CYBERTRON_END
